package com.plian.system.controller.lg;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.FileTools;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.lg.MlegalProceedingsArbitration;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.lg.IMlegalProceedingsArbitrationService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.lg.MlegalProceedingsArbitrationVO;
import com.plian.system.wrapper.lg.MlegalProceedingsArbitrationWrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * 重大法律诉讼及仲裁案件 控制器
 *
 * @author gangan
 * @since 2019-09-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/mlegalproceedingsarbitration")
public class MlegalProceedingsArbitrationController extends BaseController {

	private IMlegalProceedingsArbitrationService mlegalProceedingsArbitrationService;

	private FormWorkflowService formWorkflowService;

	private MlegalProceedingsArbitrationWrapper mlegalProceedingsArbitrationWrapper;

	/**
	 * 详情
	 */
	@PostMapping("/detail")
	public R<MlegalProceedingsArbitrationVO> detail(@RequestBody MlegalProceedingsArbitration mlegalProceedingsArbitration) {
		MlegalProceedingsArbitration detail = mlegalProceedingsArbitrationService.getOne(Condition.getQueryWrapper(mlegalProceedingsArbitration));
		MlegalProceedingsArbitrationVO mlegalProceedingsArbitrationVO = mlegalProceedingsArbitrationWrapper.entityToVO(detail);
		R result = new R<MlegalProceedingsArbitrationVO>(CommonCode.SUCCESS);
		result.setPageData(mlegalProceedingsArbitrationVO);
		return result;
	}

	/**
	 * 分页
	 */
	@PostMapping("/list")
	public ListResult<MlegalProceedingsArbitrationVO> list(@RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		if (query.getAscs()==null&&query.getDescs()==null){
			query.setDescs("create_time");
		}
		QueryWrapper<MlegalProceedingsArbitration> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new MlegalProceedingsArbitration()), entityList
		);
		TokenSearch<MlegalProceedingsArbitration> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);

		IPage<MlegalProceedingsArbitration> pages = mlegalProceedingsArbitrationService.page(Condition.getPage(query), queryWrapper);
		ListResult<MlegalProceedingsArbitrationVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		listResult.setList(mlegalProceedingsArbitrationWrapper.entityToVO(pages.getRecords()));
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;

	}


	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Resubmit(delaySeconds = 0)
	@LogOperation("重大法律诉讼及仲裁案件-提交")
	public R submit(@Valid @RequestBody MlegalProceedingsArbitration mlegalProceedingsArbitration) {
		if (mlegalProceedingsArbitrationService.checkIsExisted(mlegalProceedingsArbitration.getId(), mlegalProceedingsArbitration.getCode())) {

			boolean result = mlegalProceedingsArbitrationService.saveOrUpdate(mlegalProceedingsArbitration);
			HashMap<String, Object> processVariable = new HashMap<>();
			formWorkflowService.startProcessAndSimulate(String.valueOf(mlegalProceedingsArbitration.getId()),mlegalProceedingsArbitration.getFormType(),processVariable);

			return result ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
		} else {
			return new R(CustomCode.builder().message("单据编码已存在").code(1999).success(false).build());
		}

	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@LogOperation("重大法律诉讼及仲裁案件-删除")
	public R remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		return mlegalProceedingsArbitrationService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
				? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
	}

//	/** TODO DEL
//	 * 市属企业重大法律诉讼及仲裁案件情况调查表
//	 */
//	@PostMapping("/legalArbitrationSurvey")
//	public R<ReportReturnVO> legalArbitrationSurvey(@RequestBody MlegalProceedingsArbitrationDTO mlegalProceedingsArbitrationDTO) {
//		ReportReturnVO reportReturnVO = mlegalProceedingsArbitrationService.legalArbitrationSurvey(mlegalProceedingsArbitrationDTO);
//		R<ReportReturnVO> results = new R<>(CommonCode.SUCCESS);
//		results.setPageData(reportReturnVO);
//		return results;
//	}


	/**
	 * 打印
	 * @param fillingUnitId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@GetMapping("/buildpdf")
//	@PassToken
	public ResponseEntity<FileSystemResource> buildPdf(@RequestParam("fillingUnitId") String fillingUnitId,
													   @RequestParam("startTime") String startTime,
													   @RequestParam("endTime") String endTime){
		return FileTools.exportPdf(mlegalProceedingsArbitrationService.buildpdf(fillingUnitId, startTime, endTime));
	}
}
